package com.sta.oldman.coder;

import com.sta.oldman.constants.MsgContains;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author: LunBoWang
 * @description: 编码器
 * @date: 2025/5/25 下午8:15
 * @version: 1.0
 */

public class TcpMsgEncoder extends MessageToByteEncoder {


    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object msg, ByteBuf out) throws Exception {
        TcpMsg tcpMsg = (TcpMsg) msg;
        out.writeShort(tcpMsg.getHeader());
        out.writeByte(tcpMsg.getControl());
        out.writeByte(tcpMsg.getCommand());
        out.writeShort(tcpMsg.getLength());
        out.writeBytes(tcpMsg.getData());
        out.writeByte(tcpMsg.getSum());
        out.writeShort(tcpMsg.getTail());
        out.writeBytes(MsgContains.DEFAULT_DECODE_CHAR.getBytes());
    }
}
